package com.situ.util.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.sys.service.SysRoleService;
import com.situ.util.entity.Product;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.situ.util.service.SysUserService;
import com.situ.util.entity.SysUser;
import com.situ.util.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SysUser的路由接口服务
 * 
 * @author 
 *
 */
@RestController
public class SysUserController {

	/** SysUserService服务 */
	@Autowired
	private SysUserService sysUserService;
	@Autowired
	private SysRoleService sysRoleService;
	
	/**
	 * 查询所有SysUser数据的方法
	 * @param value
	 * @return
	 */
	@RequestMapping(value = "querySysUser", produces = {"application/json;charset=UTF-8"})
	public ResponseData find(SysUser value) {

		return ResponseData.ok().putDataValue("list",sysUserService.find(value));
	}
	
	/**
	 * 通过id查询SysUser数据的方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "querySysUserById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData findOne(@PathVariable(name="id") Integer id) {
		 return ResponseData.ok().putDataValue("obj",sysUserService.findOne(id));
	}
	
	/**
	 * 插入SysUser属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "saveSysUser")
	public ResponseData save(SysUser value) {
		return ResponseData.ok().putDataValue("obj", sysUserService.saveNotNull(value));
	}
	
	/**
	 * 更新SysUser属性不为空的数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "updateSysUser", produces = {"application/json;charset=UTF-8"})
	public ResponseData update(SysUser value) {
		return ResponseData.ok().putDataValue("obj",sysUserService.updateNotNullById(value));
	}

	/**
	 * 通过id删除SysUser数据方法
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "delSysUserById/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData delete(@PathVariable(name="id") Integer id) {
		return ResponseData.ok().putDataValue("obj",sysUserService.deleteById(id));
	}

	@RequestMapping("selectSysUserBylike")
	public ResponseData selectSysUserBylike(SysUser value,
				@RequestParam(defaultValue ="1", required = false) int startPage ,
				@RequestParam(defaultValue = "5",required = false) int PageSize){
		PageHelper.startPage(startPage,PageSize );
		List<Map> list = sysUserService.selectSysUserBylike(value);
		PageInfo pageInfo=new PageInfo(list,5);
		return ResponseData.ok().putDataValue("pageInfo",pageInfo);
	}

	@RequestMapping("savesysuser")
	public ResponseData saveSysuser(SysUser s){
		sysUserService.saveSysuser(s);
		return ResponseData.ok();
	}

	@RequestMapping("selectusrname")
	public ResponseData selectusrname(String value){
		Integer selectusrname = sysUserService.selectusrname(value);
		System.out.println(selectusrname);
		String tishi;
		if(selectusrname==0){
			tishi="用户名可用！";
		}else{
			tishi="用户名不可用！";
		}
		return ResponseData.ok().putDataValue("tishi", tishi);
	}

	@RequestMapping(value = "querySysUserById2/{id}", produces = {"application/json;charset=UTF-8"})
	public ResponseData querySysUserById2(@PathVariable(name="id") Integer id){
		return ResponseData.ok().putDataValue("obj", sysUserService.querySysUserById2(id));
	}

	@RequestMapping("queryrole")
	public ResponseData queryrole(){
		return ResponseData.ok().putDataValue("list", sysRoleService.find(null));
	}

	@RequestMapping("updaterole")
	public ResponseData updaterole(Integer id,Integer value){
		Map map = new HashMap();
		map.put("id", id);
		map.put("value", value);
		sysUserService.updaterole(map);
		return ResponseData.ok();
	}
}
